package com.farm.utils_all;

import java.lang.reflect.Method;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.TimeZone;

public class HUtil {

	public static String cerateorderno(String str, Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
		formatter.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		String orderno = formatter.format(date) + 0 + str;
		return orderno;
	}

	public static Map<String, Object> dateToString(Map<String, Object> map) {
		if (map == null)
			return new HashMap<>();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		formatter.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
		List<String> keyMap = new ArrayList<>();
		while (entries.hasNext()) {
			Map.Entry<String, Object> entry = entries.next();
			String key = entry.getKey();
			Object value = entry.getValue();
			if (value instanceof Date) {
				keyMap.add(key);
			}
		}
		for (String key : keyMap) {
			String time = formatter.format(map.get(key));
			map.remove(key);
			map.put(key, time);
		}
		return map;
	}

	public static List<Map<String, Object>> dateToString(List<Map<String, Object>> list) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		formatter.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		if (list == null)
			return new ArrayList<>();
		for (Map<String, Object> map : list) {
			Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator();
			List<String> keyMap = new ArrayList<>();
			while (entries.hasNext()) {
				Map.Entry<String, Object> entry = entries.next();
				String key = entry.getKey();
				Object value = entry.getValue();
				if (value instanceof Date) {
					keyMap.add(key);
				}
			}
			for (String key : keyMap) {
				String time = formatter.format(map.get(key));
				map.remove(key);
				map.put(key, time);
			}
		}
		return list;
	}

	public static String getRandomString(int length) {
		String str = "0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(10);
			sb.append(str.charAt(number));
		}
		return sb.toString();
	}

	public static byte[] decode(String input) throws Exception {
		Class clazz = Class.forName("com.sun.org.apache.xerces.internal.impl.dv.util.Base64");
		Method mainMethod = clazz.getMethod("decode", String.class);
		mainMethod.setAccessible(true);
		Object retObj = mainMethod.invoke(null, input);
		return (byte[]) retObj;
	}

	public static int dateCompare(String date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		df.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		try {
			Date dt1 = df.parse(date);
			Date dt2 = new Date();
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	public static int dateCompare(Date date) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm");
		df.setTimeZone(TimeZone.getTimeZone("GMT+08"));
		try {
			Date dt1 = date;
			Date dt2 = new Date();
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	public static Map<String, Object> putInMap(Object... str) {
		Map<String, Object> map = new HashMap<>();
		for (int i = 0; i < str.length; i++) {
			if (str[i + 1] != null) {
				String key = str[i].toString();
				Object value = str[i + 1];
				map.put(key, value);
			}
			i++;
		}
		return map;
	}

	public static Map<String, String> putInMapS(String... str) {
		Map<String, String> map = new HashMap<>();
		for (int i = 0; i < str.length; i++) {
			if (str[i + 1] != null) {
				String key = str[i].toString();
				String value = str[i + 1];
				map.put(key, value);
			}
			i++;
		}
		return map;
	}

	public static Map<String, Object> mapStrToObj(Map<String, String> strMap) {
		Map<String, Object> objMap = new HashMap<>();
		Iterator<Map.Entry<String, String>> entries = strMap.entrySet().iterator();
		while (entries.hasNext()) {
			Map.Entry<String, String> entry = entries.next();
			objMap.put(entry.getKey(), entry.getValue());
		}
		return objMap;
	}

}
